package cn.wen.yinxiu.works.controller;

import java.util.List;

import cn.wen.yinxiu.common.convention.result.Results;
import cn.wen.yinxiu.common.to.works.SimpleWorksTO;
import cn.wen.yinxiu.common.vo.UserWithStatusReq;
import cn.wen.yinxiu.works.entity.DiagnosisEntity;
import cn.wen.yinxiu.works.service.DiagnosisService;
import cn.wen.yinxiu.works.to.HeaderNavUserInfoTO;
import cn.wen.yinxiu.works.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import cn.wen.yinxiu.works.service.WorksService;
import cn.wen.yinxiu.common.convention.result.ResultEntity;
import org.springframework.web.multipart.MultipartFile;

/**
 * @ClassName: WorksController
 * @Author 小飞
 * @Email coderwen22@163.com
 * @Date 2023-05-22 09:39:56
 * @Description: 的Controller层
 */
@RestController
@RequestMapping("/web")
@EnableScheduling
public class WorksController {

    @Autowired
    private WorksService worksService;

    @Autowired
    private DiagnosisService diagnosisService;

    /**
     * 首页头部导航栏用户基本信息：
     * 作品数量、用户收藏数量、用户浏览记录、我的喜欢数量
     *
     * @param userId
     * @return
     */
    @GetMapping("/api/works/head-nav/count")
    public ResultEntity<HeaderNavUserInfoTO> getWorksHeadNavCount(@RequestParam("userId") Long userId) {
        HeaderNavUserInfoTO res = worksService.queryWorksHeadNavCount(userId);
        return Results.success(res);
    }

    /**
     * 获取作品的基本信息
     *
     * @return
     */
    @GetMapping("/api/works/simple/info")
    ResultEntity<SimpleWorksTO> getWorksSimpleInfo(@RequestParam("worksId") Long worksId) {
        SimpleWorksTO res = worksService.getWorksSimpleInfo(worksId);
        return Results.success(res);
    }

    /**
     * 用户的作品列表
     *
     * @param userId
     * @return
     */
    @GetMapping("/api/works/list")
    public ResultEntity<List<WorksListInfo>> getWorksListCount(@RequestParam("userId") Long userId,
                                                               @RequestParam("keyword") String keyword,
                                                               @RequestParam("page") Integer page) {
        List<WorksListInfo> res = worksService.queryWorksList(userId, keyword, page);
        return Results.success(res);
    }

    /**
     * 用户的喜欢作品
     *
     * @param userId
     * @return
     */
    @GetMapping("/api/works/like/list")
    public ResultEntity<List<WorksListInfo>> getWorksLikeCount(@RequestParam("userId") Long userId,
                                                               @RequestParam("page") Integer page,
                                                               @RequestParam("keyword") String keyword) {
        List<WorksListInfo> res = worksService.queryWorksLikeList(userId, page, keyword);
        return Results.success(res);
    }

    /**
     * 用户的收藏作品
     *
     * @param userId
     * @return
     */
    @GetMapping("/api/works/collect/list")
    public ResultEntity<List<WorksListInfo>> getWorksCollect(@RequestParam("userId") Long userId,
                                                             @RequestParam("page") Integer page) {
        List<WorksListInfo> res = worksService.queryWorksCollectList(userId, page);
        return Results.success(res);
    }

    /**
     * 用户浏览历史记录（只保存最近七天）
     *
     * @param userId
     * @return
     */
    @GetMapping("/api/works/visit/history/list")
    public ResultEntity<List<WorksListInfo>> getWorksVisitHistoryList(@RequestParam("userId") Long userId,
                                                                      @RequestParam("page") Integer page) {
        List<WorksListInfo> res = worksService.queryWorksVisitHistoryList(userId, page);
        return Results.success(res);
    }

    /**
     * 一键清除观看历史
     *
     * @param userId
     * @return
     */
    @PostMapping("/api/works/visit/history/delete")
    public ResultEntity deleteVisitHistory(@RequestParam("userId") Long userId) {
        boolean res = worksService.deleteWorksVisitHistory(userId);
        return Results.success(res);
    }

    /**
     * 通过作品状态获取作品列表信息
     *
     * @return
     */
    @GetMapping("/api/works/serve/status/list")
    public ResultEntity<List<WorksListInfo>> getServeStatusWorksList(@RequestParam("userId") Long userId,
                                                                     @RequestParam("status") Integer status,
                                                                     @RequestParam("keyword") String keyword,
                                                                     @RequestParam("page") Integer page) {
        List<WorksListInfo> res = worksService.queryWorksList(new UserWithStatusReq(userId, status, keyword, page));
        return Results.success(res);
    }

    /**
     * 发布作品
     *
     * @return
     */
    @GetMapping("/api/works/serve/add")
    public ResultEntity addWorks(@RequestPart MultipartFile[] fileList,
                                 @RequestParam("worksJson") String worksJson) {
        boolean res = worksService.addWorksInfo(fileList, worksJson);
        if (res) return Results.success();
        return Results.failure();
    }

    /**
     * 获取用户个人中心基本信息
     *
     * @param userId
     * @return
     */
    @GetMapping("/api/works/serve/home/info")
    public ResultEntity<ServeHomeInfo> getServeHomeInfo(@RequestParam("userId") Long userId) {
        // 首页默认为1天周期
        ServeHomeInfo res = worksService.getServeHomeInfo(userId, 1);
        return Results.success(res);
    }

    /**
     * 修改作品信息（包括删除功能）
     *
     * @param
     * @return
     */
    @GetMapping("/api/works/serve/update")
    public ResultEntity updateWorks(@RequestPart MultipartFile file,
                                    @RequestParam("updateWorksJson") String updateWorksVO) {
        boolean res = worksService.updateWorksById(file, updateWorksVO);
        if (!res) return Results.failure();
        return Results.success();
    }

    /**
     * 统计一天的数据
     *
     * @param
     * @return
     */
    @Scheduled(cron = "0 0 0 * *")
    @PostMapping("/api/works/admin/statistics")
    public ResultEntity statisticsWorksData() {
        boolean res = worksService.statisticsWorksData();
        if (!res) return Results.failure();
        return Results.success();
    }

    /**
     * 获取评论管理中作品的基本数据列表
     *
     * @return
     */
    @GetMapping("/api/works/comment/works/list")
    public ResultEntity<List<WorksSimpleInfo>> commentWorksList(@RequestParam("userId") Long userId) {
        List<WorksSimpleInfo> res = worksService.getCommentWorksList(userId);
        return Results.success(res);
    }

    /**
     * 账号诊断 (只统计最近五天的数据)
     *
     * @param userId
     * @return
     */
    @GetMapping("/api/works/diagnosis")
    public ResultEntity<DiagnosisEntity> getWorksDiagnosis(@RequestParam("userId") Long userId) {
        DiagnosisEntity res = diagnosisService.getWorksDiagnosis(userId);
        return Results.success(res);
    }

    /**
     * 账号诊断统计最近五天的数据定时任务（第二天的00:00执行） (只统计最近五天的数据)
     *
     * @return
     */
    @Scheduled(cron = "0 0 0 * *")
    @PostMapping("/api/works/diagnosis/timer")
    public ResultEntity doDiagnosisDataStatisticsTimer() {
        boolean res = diagnosisService.doDiagnosisDataStatisticsTimer();
        if (res) return Results.success();
        return Results.failure();
    }

    /**
     * 根据参数获取用户信息和基本信息
     *
     * @param userId    用户ID
     * @param period    时间选择周期
     * @param filedType 选择字段类型
     * @return
     */
    @GetMapping("/api/works/core/tendency")
    public ResultEntity getWorksCoreTendency(@RequestParam("userId") Long userId,
                                             @RequestParam("period") Integer period,
                                             @RequestParam("filedType") Integer filedType) {
        ServeHomeInfo res = worksService.getWorksCoreTendency(userId, period, filedType);
        return Results.success(res);
    }

    /**
     * 作品总结：
     * 分别获取近30天作品优异的列表
     * 播放量最高、点赞最高、收藏最高、评论最高
     *
     * @return
     */
    @GetMapping("/api/works/summary/list")
    public ResultEntity getWorksSummaryList(@RequestParam("userId") Long userId) {
        List<WorksSummaryVO> res = worksService.getWorksSummaryList(userId);
        return Results.success(res);
    }

    /**
     * 作品列表(包括点赞指数、评论指数、收藏指数 指数 = n / 播放数)
     *
     * @return
     */
    @GetMapping("/api/works/cart/list")
    public ResultEntity getWorksList(@RequestParam("userId") Long userId) {
        List<WorksCartVO> res = worksService.getWorksCartList(userId);
        return Results.success(res);
    }

    /**
     * 获取周报信息
     *
     * @param userId
     * @return
     */
    @GetMapping("/api/works/week/report/info")
    public ResultEntity getWorksWeekNewspaper(@RequestParam("userId") Long userId) {
        WeekReportVO res = worksService.getWeekReportInfo(userId);
        return Results.success(res);
    }

    /**
     * 定时生成周报周报信息
     *
     * @return
     */
    @PostMapping("/api/works/week/report/create")
    public ResultEntity addWorksWeekReport() {
        boolean res = worksService.createWeekReportInfo();
        if (!res) return Results.failure();
        return Results.success();
    }

    /**
     * 生成推荐ID到数据库中
     *
     * @return
     */
    @PostMapping("/api/works/recommend/create")
    public ResultEntity createRecommendList(@RequestParam("userId") Long userId) {
        boolean res = worksService.createRecommendList(userId);
        if (!res) return Results.failure();
        return Results.success();
    }

    /**
     * 获取推荐列表数据
     *
     * @return
     */
    @GetMapping("/api/works/recommend/list")
    public ResultEntity getRecommendList(@RequestParam("userId") Long userId,
                                         @RequestParam("page") Integer page) {
        List<WorksVO> worksList = worksService.getRecommendList(userId, page);
        return Results.success(worksList);
    }

    /**
     * 获取随机的数据列表(为了避免数据为空则需要使用随机数据)
     *
     * @return
     */
    @GetMapping("/api/works/random/list")
    public ResultEntity getRandomWorksList() {
        List<WorksVO> worksList = worksService.getRandomWorksList();
        return Results.success(worksList);
    }

    /**
     * 获取随机的数据列表(为了避免数据为空则需要使用随机数据)
     *
     * @return
     */
    @GetMapping("/api/works/simple/random/list")
    public ResultEntity getSimpleRandomWorksList() {
        List<WorksSimpleInfo> worksList = worksService.getSimpleRandomWorksList();
        return Results.success(worksList);
    }

    /**
     * 获取首页列表的数据：
     * 随机在流量库
     *
     * @return
     */
    @GetMapping("/api/works/home/list")
    public ResultEntity getHomeList(@RequestParam("userId") Long userId) {
        List<WorksSimpleInfo> worksList = worksService.getHomeList(userId);
        return Results.success(worksList);
    }

    /**
     * 关注的用户数据列表
     *
     * @return
     */
    @GetMapping("/api/works/follow/list")
    public ResultEntity getFollowList(@RequestParam("userId") Long userId) {
        List<WorksVO> worksList = worksService.getFollowList(userId);
        return Results.success(worksList);
    }

    /**
     * 朋友的用户数据列表
     *
     * @return
     */
    @GetMapping("/api/works/friend/list")
    public ResultEntity getFriendWorksList(@RequestParam("userId") Long userId) {
        List<WorksVO> worksList = worksService.getFriendList(userId);
        return Results.success(worksList);
    }

    /**
     * 热点作品数据
     *
     * @return
     */
    @GetMapping("/works/hot/list")
    public ResultEntity getHotWorksList(@RequestParam("userId") Long userId) {
        List<WorksSimpleInfo> worksList = worksService.getHotWorksList(userId);
        return Results.success(worksList);
    }

    /**
     * 通过分类ID 获取分类列表
     *
     * @return
     */
    @GetMapping("/api/works/type/list")
    public ResultEntity getTypeWorksList(@RequestParam("userId") Long userId,
                                         @RequestParam("typeId") Long typeId) {
        List<WorksSimpleInfo> worksList = worksService.getTypeWorksList(userId, typeId);
        return Results.success(worksList);
    }

    /**
     * 作品点赞
     *
     * @return
     */
    @PostMapping("/api/works/like")
    public ResultEntity worksLike(@RequestParam("worksId") Long worksId,
                                  @RequestParam("userId") Long userId) {
        boolean res = worksService.worksLikeById(worksId, userId);
        if (res) return Results.success();
        return Results.failure();
    }

    /**
     * 作品取消点赞
     *
     * @return
     */
    @PostMapping("/api/works/unlike")
    public ResultEntity worksUnlike(@RequestParam("worksId") Long worksId,
                                    @RequestParam("userId") Long userId) {
        boolean res = worksService.worksUnlikeById(worksId, userId);
        if (res) return Results.success();
        return Results.failure();
    }


    /**
     * 作品点赞
     *
     * @return
     */
    @PostMapping("/api/works/collect")
    public ResultEntity worksCollect(@RequestParam("worksId") Long worksId,
                                     @RequestParam("userId") Long userId) {
        boolean res = worksService.worksCollectById(worksId, userId);
        if (res) return Results.success();
        return Results.failure();
    }

    /**
     * 作品取消收藏
     *
     * @return
     */
    @PostMapping("/api/works/cancel/collect")
    public ResultEntity worksCancelCollect(@RequestParam("worksId") Long worksId,
                                           @RequestParam("userId") Long userId) {
        boolean res = worksService.worksCancelCollectById(worksId, userId);
        if (res) return Results.success();
        return Results.failure();
    }

    /**
     * 将商品ID存入到作品中
     *
     * @return
     */
    @PostMapping("/web/api/works/product/save")
    ResultEntity addWorksProduct(@RequestParam("worksId") Long worksId,
                                 @RequestParam("productId") Long productId) {
        boolean res = worksService.saveWorksProductId(worksId, productId);
        if (res) return Results.success();
        return Results.failure();

    }
    /**
     * 将商品ID从指定的作品中移除
     *
     * @param worksId
     * @return
     */
    @PostMapping("/web/api/works/product/delete")
    ResultEntity deleteWorksProductById(@RequestParam("worksId") Long worksId) {
        boolean res = worksService.deleteWorksProductId(worksId);
        if (res) return Results.success();
        return Results.failure();
    }

    /**
     * 将商品ID存入到作品中
     * @return
     */
    @PostMapping("/web/api/works/advert/save")
    ResultEntity addWorksAdvert(@RequestParam("worksId") Long worksId,
                                @RequestParam("advertId") Long advertId) {
        boolean res = worksService.saveWorksAdvertId(worksId, advertId);
        if (res) return Results.success();
        return Results.failure();
    }

    /**
     * 将商品ID从指定的作品中移除
     *
     * @param worksId
     * @return
     */
    @PostMapping("/web/api/works/advert/delete")
    ResultEntity deleteWorksAdvertById(@RequestParam("worksId") Long worksId) {
        boolean res = worksService.deleteWorksAdvertId(worksId);
        if (res) return Results.success();
        return Results.failure();
    }


}
